/**
 * Created by xuelei.kong on 2016/4/28.
 */
$(function () {
    function sortColumns() {
        var unSort=$('#dataTable').data('sort').split(",");
        var columnsLen=$('#dataTable').data("columns"),aoColumns=[];
        for(var i=0;i<columnsLen;i++){
            if(unSort[i]){
                aoColumns.push({"bSortable": false});
            }else{
                aoColumns.push(null);
            }
        }
        return aoColumns;
    }

    $('#dataTable').dataTable({
        "bAutoWidth":false,
        "bInfo": false,
        "bFilter": false,
        "bPaginate": false,
        "bScrollCollapse": true,
        //TODO 动态根据列数生产排序规则
        "aoColumns":sortColumns()
    });
    function setPeopleListHeight() {
        $('.table-responsive').height(document.documentElement.clientHeight * 0.7);
    }

    function winResize(fn) {
        var timer;
        clearTimeout(timer);
        $(window).resize(function () {
            timer = setTimeout(fn, 300);
        });
    }

    setPeopleListHeight();
    winResize(setPeopleListHeight);
    $('table th input:checkbox').on('click', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox')
            .each(function () {
                this.checked = that.checked;
                $(this).closest('tr').toggleClass('selected');
            });

    });

    $('#dataTableWrap tbody .ace').on("click", function () {
        var flag = true;
        $(this).closest('tr').toggleClass('selected');
        $('#dataTableWrap tbody .ace').each(function (i) {
            if (!this.checked) {
                flag = false;
                return false;
            }
        })
        $('.check_all .ace')[0].checked = flag;
    })

    $("#delete_bash").on("click", function () {
        if ($('#dataTableWrap tbody .ace:checked').length) {
            bootbox.confirm({
                buttons: {
                    confirm: {
                        label: '确定'
                    },
                    cancel: {
                        label: '取消'
                    }
                },
                message: "确定删除?",
                callback: function (result) {
                    if (result) {
                        $('#dataTableWrap tbody .ace:checked').each(function () {
                            $(this).parents('tr').remove();
                        })
                    }
                }
            });
        }

    });
    /*单个删除*/
    $('.delete-btn').on("click", function () {
        var tr = $(this).parents('tr');
        bootbox.confirm({
            buttons: {
                confirm: {
                    label: '确定'
                },
                cancel: {
                    label: '取消'
                }
            },
            message: "确定删除?",
            callback: function (result) {
                if (result) {
                    tr.remove();
                }
            }
        });
    })
    /*查看*/
    $('.view-btn').on("click", function () {
        $.get('people_detail.html',{"userID":1},function (data) {
            bootbox.dialog({
                message: data,
                buttons
                    :
                {
                    "success" :
                    {
                        "label" : "<i class='icon-ok'></i> Close",
                        "className" : "btn-sm btn-success",
                        "callback" : function () {
                                //Example.show("great success");
                            }
                    }
                }
            })
        },"html");

        ;
    })

//翻页
    var totalPage = 30;

    function pagination(total, curr) {
        var page_btn = '<div class="pagination">';
        if (curr == 1) {
            page_btn += '<a class="pg-prev pg-prev-disabled"><i class="icon-double-angle-left"></i></a>';
        } else {
            page_btn += '<a data-page="' + (curr - 1) + '" href="javascript:void(0)" class="pg-prev"><i class="icon-double-angle-left"></i></a>';
        }
        if (curr - 1 > 3) {
            page_btn += '<a data-page="1" href="javascript:void(0)" class="pg-page pg-item">1</a>';
            page_btn += '<span class="pg-item">…</span>';
            for (var j = curr - 2; j < curr; j++) {
                page_btn += '<a data-page="' + j + '" href="javascript:void(0)" class="pg-page pg-item">' + j + '</a>';
            }
        } else {
            for (var i = 1; i < curr; i++) {
                page_btn += '<a data-page="' + i + '" href="javascript:void(0)" class="pg-page pg-item">' + i + '</a>';
            }

        }
        page_btn += '<span class="pg-current pg-item">' + curr + '</span>';
        if (curr + 3 > total) {
            for (var k = curr; k < total; k++) {
                page_btn += '<a data-page="' + (k + 1) + '" href="javascript:void(0)" class="pg-page pg-item">' + (k + 1) + '</a>';
            }
        } else {
            page_btn += '<a data-page="' + (curr + 1) + '" href="javascript:void(0)" class="pg-page pg-item">' + (curr + 1) + '</a>';
            page_btn += '<a data-page="' + (curr + 2) + '" href="javascript:void(0)" class="pg-page pg-item">' + (curr + 2) + '</a>';
            if (curr + 3 < total) {
                page_btn += '<span class="pg-item">…</span>';
            }
            page_btn += '<a data-page="' + total + '" href="javascript:void(0)" class="pg-page pg-item">' + total + '</a>';

        }

        if (curr == total) {
            page_btn += '<a class="pg-next pg-next-disabled"><i class="icon-double-angle-right"></i></a>';
        } else {
            page_btn += '<a data-page="' + (curr + 1) + '" href="javascript:void(0)" class="pg-next"><i class="icon-double-angle-right"></i></a>';
        }
        page_btn += "</div>"
        $('#pagination').empty().append(page_btn);

    }

    pagination(totalPage, 1);
    $('#pagination').on('click', "a", function () {
        var page = $(this).data('page');
        page && pagination(totalPage, page);
    })
})


